cap log close
log using "$results/3b_ipums_analysis_tables.log", replace
	
************************************
**# Tables 5 & A11: Baseline tables 		  
************************************
qui {
**# Setup		    
use "analysis/ipums_industry_data.dta", clear
merge 1:1 ind1990 year using "analysis/ind1990_changes_rout1"
	* m=2: inds in ipums emp data that not in industry level data
	* 122
	* 140
	* 301
	* 350
	* 392
assert _m!=1
keep if _m == 3
drop _merge
drop if inlist(ind1990,210,232,362,390)
 
glo IV "ave_ols_ch  = ave_iv_ch"

xtset ind1990 year
		 
global tariff_controls  dln_pstar_vw_7279_alt mfa rho other_tariff_change
global prod_controls	ln_cap_lab_78 skill_emp_share_78 women_ind_share mat_ship_def_78
global rti_controls		ln_invest_def_78 lag_invest_def_ch rti automation78

loc controls1 
loc controls2 `controls1' $tariff_controls
loc controls3 `controls2' $prod_controls
loc controls4 `controls3' $rti_controls

loc ctrl1_lab "None"
loc ctrl2_lab "+Trade"
loc ctrl3_lab "+Prod."
loc ctrl4_lab "+Tech."

egen full = rowmiss(`controls4')
keep if full==0
drop full
		 
**# Regressions		 
eststo clear

loc out pay 

sum ave_ols_ch,d
loc iqrx `=`r(p75)'-`r(p25)'' 
forval i = 1/4 {
		
**## Pooled sample				
	reg  ave_ols_ch ave_iv_ch   `controls`i'', robust	
	loc fsb = _b[ave_iv_ch]

	sum ln_skill_diff_`out'_ch, d
	loc iqry `=`r(p75)'-`r(p25)''

	eststo skill_`out'`i': ivreg2 ln_skill_diff_`out'_ch ($IV) `controls`i'', robust	
	eststo skill_`out'`i', add(UnderID_p  e(idp))
	eststo skill_`out'`i', add(Weak_IV_KP e(widstat))
	eststo skill_`out'`i', add(FSB        `fsb')
	loc beta = _b[ave_ols_ch]
	loc iqe = `=(`beta'*`iqrx') '
	estadd  scalar IQR = `iqe'
	estadd loc spec "IV"
	estadd loc controls "`ctrl`i'_lab'"
					
**## Men sample

	sum m_ln_skill_diff_`out'_ch, d
	loc iqry `=`r(p75)'-`r(p25)''
		
	eststo m_skill_`out'`i': ivreg2 m_ln_skill_diff_`out'_ch ($IV) `controls`i'', robust	
	eststo m_skill_`out'`i', add(UnderID_p  e(idp))
	eststo m_skill_`out'`i', add(Weak_IV_KP e(widstat))
	eststo m_skill_`out'`i', add(FSB        `fsb')
	loc beta = _b[ave_ols_ch]
	loc iqe = `=(`beta'*`iqrx') '
	estadd  scalar IQR = `iqe'
	estadd loc spec "IV"
	estadd loc controls "Full"

	sum w_ln_skill_diff_`out'_ch, d
	loc iqry `=`r(p75)'-`r(p25)''
 
**## Women sample
	eststo w_skill_`out'`i': ivreg2 w_ln_skill_diff_`out'_ch ($IV) `controls`i'', robust	
	eststo w_skill_`out'`i', add(UnderID_p  e(idp))
	eststo w_skill_`out'`i', add(Weak_IV_KP e(widstat))
	eststo w_skill_`out'`i', add(FSB        `fsb')
	loc beta = _b[ave_ols_ch]
	loc iqe = `=(`beta'*`iqrx') '
	estadd  scalar IQR = `iqe'
	estadd loc spec "IV"
	estadd loc controls "Full"

	}	
			
**# Make tables

**## Table 5: Baseline
noi di ""
noi di "**************************************************"
noi di "****************** Table 5 *********************"
noi di "**************************************************"
noi esttab skill_`out'4 m_skill_`out'4 w_skill_`out'4, keep(ave_ols_ch) se(%9.3f) b(%9.3f) star(* .1  ** .05 *** .01) ///
	stats( IQR spec N FSB  Weak_IV_KP,  ///
	label(	  /// 
	"IQE" "Estimator" "Obs." "\$1^{st}\$ Stage Coeff." "KP F-Stat" ) ///
	fmt(  %04.3fc  %3s  %9.0fc   %04.3fc   %04.3fc ))

esttab  skill_`out'4 m_skill_`out'4 w_skill_`out'4    ///
using "$results/table_5.tex", ///
se(%9.3f) ///
b(%9.3f) ///
star(* .1  ** .05 *** .01) ///
stats( IQR spec N FSB  Weak_IV_KP,  ///
label(	  /// 
"IQE" "Estimator" "Obs." "\$1^{st}\$ Stage Coeff." "KP F-Stat" ) ///
fmt(  %04.3fc  %3s  %9.0fc   %04.3fc   %04.3fc ) ///
) ///	
keep(ave_ols_ch) ///
mtitles("All" "Men" "Women") ///
booktabs ///
substitute(\_ _) ///
label ///
nonotes /// 
replace
	
**## Table A11: Baseline by gender & controls
noi di ""
noi di "**************************************************"
noi di "****************** Table A11 *********************"
noi di "**************************************************"
noi di ""
noi di "***************************"
noi di "******** Panel A **********"
noi di "***************************"
noi esttab  skill_`out'*, keep(ave_ols_ch) se(%9.3f) b(%9.3f) star(* .1  ** .05 *** .01) ///
	stats( IQR,	label("IQE") fmt(  %04.3fc  ))			

noi di ""
noi di "***************************"
noi di "******** Panel B **********"
noi di "***************************"
noi esttab  m_skill_`out'*, keep(ave_ols_ch) se(%9.3f) b(%9.3f) star(* .1  ** .05 *** .01) ///
	stats( IQR,	label("IQE") fmt(  %04.3fc  ))			

noi di ""
noi di "***************************"
noi di "******** Panel C **********"
noi di "***************************"
noi esttab  w_skill_`out'*, keep(ave_ols_ch) se(%9.3f) b(%9.3f) star(* .1  ** .05 *** .01) ///
	stats( IQR spec controls N FSB  Weak_IV_KP,  ///
	label(	  /// 
	"IQE" "Estimator" "Controls" "Obs." "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
	fmt(  %04.3fc  %3s  %5s   %9.0fc   %04.3fc ))			


* Panel A
esttab  skill_`out'*  using "$results/table_A11_panelA.tex", ///
se(%9.3f) ///
b(%9.3f) ///
star(* .1  ** .05 *** .01) ///
stats( IQR spec controls N FSB  Weak_IV_KP,  ///
label(	  /// 
"IQE" "Estimator" "Controls" "Obs." "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
fmt(  %04.3fc  %3s  %5s   %9.0fc   %04.3fc ) ///
) ///			
keep(ave_ols_ch) ///
nomtitles ///
nonum ///
mgroups("Panel A:  All Workers ", ///    
prefix(\multicolumn{@span}{c}{) suffix(}) ///
span erepeat(\cline{@span}) /// 
) ///
booktabs ///
substitute(\_ _) ///
label ///
nonotes /// 
replace
	
* Panel B
esttab  m_skill_`out'*  using "$results/table_A11_panelB.tex", ///
se ///
star(* .1  ** .05 *** .01) ///
stats( IQR spec controls N FSB  Weak_IV_KP,  ///
label(	  /// 
"IQE" "Estimator" "Controls" "Obs." "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
fmt(  %04.3fc  %3s  %5s   %9.0fc   %04.3fc ) ///
) ///			
keep(ave_ols_ch) ///
nomtitles ///
nonum ///
mgroups("Panel B: Male Workers ", ///    
prefix(\multicolumn{@span}{c}{) suffix(}) ///
span erepeat(\cline{@span}) /// 
) ///
booktabs ///
substitute(\_ _) ///
label ///
nonotes /// 
replace
	
* Panel C	
esttab  w_skill_`out'*  using "$results/table_A11_panelC.tex", ///
se ///
star(* .1  ** .05 *** .01) ///
stats( IQR spec controls N FSB  Weak_IV_KP,  ///
label(	  /// 
"IQE" "Estimator" "Controls" "Obs." "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
fmt(  %04.3fc  %3s  %5s   %9.0fc   %04.3fc ) ///
) ///		
keep(ave_ols_ch) ///
nomtitles ///
nonum ///
mgroups("Panel C:  Female Workers ", ///    
prefix(\multicolumn{@span}{c}{) suffix(}) ///
span erepeat(\cline{@span}) /// 
) ///
booktabs ///
substitute(\_ _) ///
label ///
nonotes /// 
replace


}
	
	
****************************************	
**# Table 6: Pay inequality/gender/tech
****************************************
qui {
**# Panel A: Gender & routineness

**## Setup
glo IV "ave_ols_ch  = ave_iv_ch"
forval rout = 2/3{
	use "analysis/ipums_industry_data.dta", clear
	merge 1:1 ind1990 year using "analysis/ind1990_changes_rout`rout'.dta"
	* m=2: inds in ipums emp data that not in industry level data
	* 122
	* 140
	* 301
	* 350
	* 392
	assert _m!=1
	keep if _m == 3
	drop _merge
	drop if inlist(ind1990,210,232,362,390)

	glo IV "ave_ols_ch  = ave_iv_ch"

	xtset ind1990 year

	loc controls1 
	loc controls2 `controls1' $tariff_controls
	loc controls3 `controls2' $prod_controls
	loc controls4 `controls3' $rti_controls

	egen full = rowmiss(`controls4')
	keep if full==0
	drop full

	**## Regressions

	loc out pay

	sum ave_ols_ch,d
	loc iqrx `=`r(p75)'-`r(p25)'' 

	loc i = 4
	
	* All		
	reg  ave_ols_ch ave_iv_ch   `controls`i'', robust	
	loc fsb = _b[ave_iv_ch]

	sum ln_skill_diff_`out'_ch, d
	loc iqry `=`r(p75)'-`r(p25)''
	
	eststo skill_`out'`i'_`rout': ivreg2 ln_skill_diff_`out'_ch ($IV) `controls`i'', robust	
	eststo skill_`out'`i'_`rout', add(UnderID_p  e(idp))
	eststo skill_`out'`i'_`rout', add(Weak_IV_KP e(widstat))
	eststo skill_`out'`i'_`rout', add(FSB        `fsb')
	loc beta = _b[ave_ols_ch]
	loc iqe = `=(`beta'*`iqrx') '
	estadd  scalar IQR = `iqe'
	estadd loc spec "IV"
	estadd loc controls "Full"


	* Men
	sum m_ln_skill_diff_`out'_ch, d
	loc iqry `=`r(p75)'-`r(p25)''
		
	eststo m_skill_`out'`i'_`rout': ivreg2 m_ln_skill_diff_`out'_ch  ($IV) `controls`i'', robust	
	eststo m_skill_`out'`i'_`rout', add(UnderID_p  e(idp))
	eststo m_skill_`out'`i'_`rout', add(Weak_IV_KP e(widstat))
	eststo m_skill_`out'`i'_`rout', add(FSB        `fsb')
	loc beta = _b[ave_ols_ch]
	loc iqe = `=(`beta'*`iqrx') '
	estadd  scalar IQR = `iqe'
	estadd loc spec "IV"
	estadd loc controls "Full"

	sum w_ln_skill_diff_`out'_ch, d
	loc iqry `=`r(p75)'-`r(p25)''

	* Women
	eststo w_skill_`out'`i'_`rout': ivreg2 w_ln_skill_diff_`out'_ch ($IV) `controls`i'', robust	
	eststo w_skill_`out'`i'_`rout', add(UnderID_p  e(idp))
	eststo w_skill_`out'`i'_`rout', add(Weak_IV_KP e(widstat))
	eststo w_skill_`out'`i'_`rout', add(FSB        `fsb')
	loc beta = _b[ave_ols_ch]
	loc iqe = `=(`beta'*`iqrx') '
	estadd  scalar IQR = `iqe'
	estadd loc spec "IV"
	estadd loc controls "Full"
}


**## Make table
noi di ""
noi di "**************************************************"
noi di "******************* Table 6 **********************"
noi di "**************************************************"
noi di ""
noi di "***************************"
noi di "******** Panel A **********"
noi di "***************************"
noi esttab  skill_`out'`i'_2 m_skill_`out'`i'_2 w_skill_`out'`i'_2 skill_`out'`i'_3 m_skill_`out'`i'_3 w_skill_`out'`i'_3,    ///
	keep(ave_ols_ch) se(%9.3f) b(%9.3f) star(* .1  ** .05 *** .01) ///
	stats( IQR spec N  FSB  Weak_IV_KP,  ///
	label("IQE"	"Estimator"  /// 
	"Obs."   "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
	fmt(%04.3fc %3s %9.0fc    %04.3fc  %04.3fc )) 

* Panel A (LHS)
esttab  skill_`out'`i'_2 m_skill_`out'`i'_2 w_skill_`out'`i'_2    ///
using "$results/table_6_panelA_LHS.tex", ///
se ///
star(* .1  ** .05 *** .01) ///
stats( IQR spec N  FSB  Weak_IV_KP,  ///
label("IQE"	"Estimator"  /// 
"Obs."   "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
fmt(%04.3fc %3s %9.0fc    %04.3fc  %04.3fc ) ///
) ///	0
keep(ave_ols_ch) ///
mtitles("All" "Men" "Women") ///
nonum ///
mgroups("Panel A (LHS): Routine  Occupations", ///    
prefix(\multicolumn{@span}{c}{) suffix(}) ///
span erepeat(\cline{@span}) /// 
) ///
booktabs ///
substitute(\_ _) ///
label ///
nonotes /// 
replace 

* Panel A (RHS)
esttab  skill_`out'`i'_3 m_skill_`out'`i'_3 w_skill_`out'`i'_3    ///
using "$results/table_6_panelA_RHS.tex", ///
se ///
star(* .1  ** .05 *** .01) ///
keep(ave_ols_ch) ///
stats( IQR spec N  FSB  Weak_IV_KP,  ///
label(	"IQE" "Estimator"  /// 
"Obs."   "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
fmt(%04.3fc  %3s %9.0fc   %04.3fc   %04.3fc ) ///
) ///		
mtitles("All" "Men" "Women") ///
nonum ///
mgroups("Panel B: Non-Routine  Occupations", ///
prefix(\multicolumn{@span}{c}{) suffix(}) ///
span erepeat(\cline{@span}) /// 
) ///
booktabs ///
substitute(\_ _) ///
label ///
nonotes /// 
replace

**# Panel B: Gender & automation

**## Setup	 
glo IV "ave_ols_ch  = ave_iv_ch"
use "analysis/ipums_industry_data.dta", clear
merge 1:1 ind1990 year using "analysis/ind1990_changes_rout1.dta"
* m=2: inds in ipums emp data that not in industry level data
* 122
* 140
* 301
* 350
* 392
assert _m!=1
keep if _m == 3
drop _merge
drop if inlist(ind1990,210,232,362,390)
xtset ind1990 year

loc controls1 
loc controls2 `controls1' $tariff_controls
loc controls3 `controls2' $prod_controls
loc controls4 `controls3' $rti_controls

egen full = rowmiss($controls4)
keep if full==0
drop full

**## Regressions

eststo clear	

forval hilo = 0/1 {
	loc label0 "Panel B (LHS): Low Automation"
	loc label1 "Panel B (RHS): High Automation"

	loc out pay
	sum ave_ols_ch,d
	loc iqrx `=`r(p75)'-`r(p25)'' 

	loc i = 4
	reg  ave_ols_ch ave_iv_ch   `controls`i''  if automation==`hilo', robust	
	loc fsb = _b[ave_iv_ch]

	* All 
	sum ln_skill_diff_`out'_ch if automation==`hilo', d
	loc iqry `=`r(p75)'-`r(p25)''

	eststo ashare1_`hilo': ivreg2 ln_skill_diff_`out'_ch ($IV) `controls`i''  if automation==`hilo', robust	
	eststo ashare1_`hilo', add(UnderID_p  e(idp))
	eststo ashare1_`hilo', add(Weak_IV_KP e(widstat))
	eststo ashare1_`hilo', add(FSB        `fsb')
	loc beta = _b[ave_ols_ch]
	loc iqe = `=(`beta'*`iqrx')'
	estadd  scalar IQR = `iqe'
	estadd loc spec "IV"
	estadd loc controls "Full"

	* Men 
	sum m_ln_skill_diff_`out'_ch if automation==`hilo', d
	loc iqry `=`r(p75)'-`r(p25)''

	eststo ashare2_`hilo': ivreg2 m_ln_skill_diff_`out'_ch ($IV) `controls`i''  if automation==`hilo', robust	
	eststo ashare2_`hilo', add(UnderID_p  e(idp))
	eststo ashare2_`hilo', add(Weak_IV_KP e(widstat))
	eststo ashare2_`hilo', add(FSB        `fsb')
	loc beta = _b[ave_ols_ch]
	loc iqe = `=(`beta'*`iqrx')'
	estadd  scalar IQR = `iqe'
	estadd loc spec "IV"
	estadd loc controls "Full"

	* Women
	sum w_ln_skill_diff_`out'_ch if automation==`hilo', d
	loc iqry `=`r(p75)'-`r(p25)''

	eststo  ashare3_`hilo': ivreg2 w_ln_skill_diff_`out'_ch ($IV) `controls`i''  if automation==`hilo', robust	
	eststo  ashare3_`hilo', add(UnderID_p  e(idp))
	eststo  ashare3_`hilo', add(Weak_IV_KP e(widstat))
	eststo  ashare3_`hilo', add(FSB        `fsb')
	loc beta = _b[ave_ols_ch]
	loc iqe = `=(`beta'*`iqrx')'
	estadd  scalar IQR = `iqe'
	estadd loc spec "IV"
	estadd loc controls "Full"
	
 }

 **## Make table			
noi di ""
noi di "***************************"
noi di "******** Panel B **********"
noi di "***************************"
noi	esttab  ashare1_1 ashare2_1 ashare3_1 ashare1_0 ashare2_0 ashare3_0, ///
	keep(ave_ols_ch) se(%9.3f) b(%9.3f) star(* .1  ** .05 *** .01) ///
	stats(IQR  spec N  FSB  Weak_IV_KP,  ///
	label("IQE"	"Estimator"  /// 
	"Obs."   "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
	fmt( %04.3fc  %3s %9.0fc  %04.3fc  %04.3fc ))	

* Panel B (LHS)
esttab   ashare1_1 ashare2_1 ashare3_1  ///
using "$results/table_6_panelB_LHS.tex", ///
se ///
star(* .1  ** .05 *** .01) ///
stats(IQR  spec N  FSB  Weak_IV_KP,  ///
label("IQE"	"Estimator"  /// 
"Obs."   "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
fmt( %04.3fc  %3s %9.0fc  %04.3fc  %04.3fc ) ///
) ///	0
keep(ave_ols_ch) ///
mtitles("All" "Men" "Women") ///
nonum ///
mgroups("`label`hilo''", ///    
prefix(\multicolumn{@span}{c}{) suffix(}) ///
span erepeat(\cline{@span}) /// 
) ///
booktabs ///
substitute(\_ _) ///
label ///
nonotes /// 
replace

* Panel B (RHS)
esttab   ashare1_0 ashare2_0 ashare3_0  ///
using "$results/table_6_panelB_RHS.tex", ///
se ///
star(* .1  ** .05 *** .01) ///
stats(IQR  spec N  FSB  Weak_IV_KP,  ///
label("IQE"	"Estimator"  /// 
"Obs."   "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
fmt( %04.3fc  %3s %9.0fc  %04.3fc  %04.3fc ) ///
) ///	0
keep(ave_ols_ch) ///
mtitles("All" "Men" "Women") ///
nonum ///
mgroups("`label`hilo''", ///    
prefix(\multicolumn{@span}{c}{) suffix(}) ///
span erepeat(\cline{@span}) /// 
) ///
booktabs ///
substitute(\_ _) ///
label ///
nonotes /// 
replace

}		


********************************************		
**# Table A12: Pre-trends/placebo by gender
********************************************
qui {
**# Setup
use "analysis/ipums_industry_data.dta", clear
merge 1:1 ind1990 year using "analysis/ind1990_changes_rout1"
* m=2: inds in ipums emp data that not in industry level data
* 122
* 140
* 301
* 350
* 392
assert _m!=1
keep if _m == 3
drop _merge
drop if inlist(ind1990,210,232,362,390)

glo IV "ave_ols_ch  = ave_iv_ch"

xtset ind1990 year

loc controls1 
loc controls2 `controls1' $tariff_controls
loc controls3 `controls2' $prod_controls
loc controls4 `controls3' $rti_controls

egen full = rowmiss(`controls4')
keep if full==0
drop full


**# Regressions

eststo clear	

loc out pay 

sum ave_ols_ch,d
loc iqrx `=`r(p75)'-`r(p25)'' 

label var l_ln_skill_diff_`out'_ch	"\$ \Delta \ln(\frac{Pay_i^{Non-Prod,A}}{Pay_i^{Prod,A}})_{t-1} \$ "
label var l_m_ln_skill_diff_`out'_ch	"\$ \Delta \ln(\frac{Pay_i^{Non-Prod,M}}{Pay_i^{Prod,M}})_{t-1} \$ "
label var l_w_ln_skill_diff_`out'_ch	"\$ \Delta \ln(\frac{Pay_i^{Non-Prod,W}}{Pay_i^{Prod,W}})_{t-1} \$ "

loc i = 4

* All
reg  ave_ols_ch ave_iv_ch   `controls`i'', robust	
loc fsb = _b[ave_iv_ch]

loc pre l_
sum `pre'ln_skill_diff_`out'_ch, d
loc iqry `=`r(p75)'-`r(p25)''

eststo skill_`out'`i': ivreg2 `pre'ln_skill_diff_`out'_ch (ave_ols_ch  = ave_iv_ch) `controls`i'', robust	
eststo skill_`out'`i', add(UnderID_p  e(idp))
eststo skill_`out'`i', add(Weak_IV_KP e(widstat))
eststo skill_`out'`i', add(FSB        `fsb')
loc beta = _b[ave_ols_ch]
loc iqe = `=(`beta'*`iqrx') '
estadd  scalar IQR = `iqe'
estadd loc spec "IV"
estadd loc controls "Full"

* Men
sum `pre'm_ln_skill_diff_`out'_ch, d
loc iqry `=`r(p75)'-`r(p25)''

eststo m_skill_`out'`i': ivreg2 `pre'm_ln_skill_diff_`out'_ch (ave_ols_ch  = ave_iv_ch) `controls`i'', robust	
eststo m_skill_`out'`i', add(UnderID_p  e(idp))
eststo m_skill_`out'`i', add(Weak_IV_KP e(widstat))
eststo m_skill_`out'`i', add(FSB        `fsb')
loc beta = _b[ave_ols_ch]
loc iqe = `=(`beta'*`iqrx') '
estadd  scalar IQR = `iqe'
estadd loc spec "IV"
estadd loc controls "Full"


* Women
sum `pre'w_ln_skill_diff_`out'_ch, d
loc iqry `=`r(p75)'-`r(p25)''

eststo w_skill_`out'`i': ivreg2 `pre'w_ln_skill_diff_`out'_ch (ave_ols_ch  = ave_iv_ch) `controls`i'', robust	
eststo w_skill_`out'`i', add(UnderID_p  e(idp))
eststo w_skill_`out'`i', add(Weak_IV_KP e(widstat))
eststo w_skill_`out'`i', add(FSB        `fsb')
loc beta = _b[ave_ols_ch]
loc iqe = `=(`beta'*`iqrx') '
estadd  scalar IQR = `iqe'
estadd loc spec "IV"
estadd loc controls "Full"

/// Controlling for lag
* All
reg  ave_ols_ch ave_iv_ch     `pre'ln_skill_diff_`out'_ch  `controls`i'', robust	
loc fsb = _b[ave_iv_ch]
eststo lskill_`out'`i': ivreg2  ln_skill_diff_`out'_ch `pre'ln_skill_diff_`out'_ch (ave_ols_ch  = ave_iv_ch) `controls`i'', robust	
eststo lskill_`out'`i', add(UnderID_p  e(idp))
eststo lskill_`out'`i', add(Weak_IV_KP e(widstat))
eststo lskill_`out'`i', add(FSB        `fsb')
loc beta = _b[ave_ols_ch]
loc iqe = `=(`beta'*`iqrx') '
estadd  scalar IQR = `iqe'
estadd loc spec "IV"
estadd loc controls "Full"

* Men
reg  ave_ols_ch ave_iv_ch     `pre'm_ln_skill_diff_`out'_ch  `controls`i'', robust	
loc fsb = _b[ave_iv_ch]

eststo lm_skill_`out'`i': ivreg2  m_ln_skill_diff_`out'_ch `pre'm_ln_skill_diff_`out'_ch  (ave_ols_ch  = ave_iv_ch) `controls`i'', robust	
eststo lm_skill_`out'`i', add(UnderID_p  e(idp))
eststo lm_skill_`out'`i', add(Weak_IV_KP e(widstat))
eststo lm_skill_`out'`i', add(FSB        `fsb')
loc beta = _b[ave_ols_ch]
loc iqe = `=(`beta'*`iqrx') '
estadd  scalar IQR = `iqe'
estadd loc spec "IV"
estadd loc controls "Full"


* Women
reg  ave_ols_ch ave_iv_ch     `pre'w_ln_skill_diff_`out'_ch  `controls`i'', robust	
loc fsb = _b[ave_iv_ch]

eststo lw_skill_`out'`i': ivreg2 w_ln_skill_diff_`out'_ch `pre'w_ln_skill_diff_`out'_ch (ave_ols_ch  = ave_iv_ch)  `controls`i'', robust	
eststo lw_skill_`out'`i', add(UnderID_p  e(idp))
eststo lw_skill_`out'`i', add(Weak_IV_KP e(widstat))
eststo lw_skill_`out'`i', add(FSB        `fsb')
loc beta = _b[ave_ols_ch]
loc iqe = `=(`beta'*`iqrx') '
estadd  scalar IQR = `iqe'
estadd loc spec "IV"
			
**# Make table 			
noi di ""
noi di "**************************************************"
noi di "******************* Table A12 *********************"
noi di "**************************************************"
noi di ""
noi di "***************************"
noi di "******** Panel A **********"
noi di "***************************"
noi esttab  skill_`out'`i' m_skill_`out'`i' w_skill_`out'`i',    ///
	keep(ave_ols_ch) se(%9.3f) b(%9.3f) star(* .1  ** .05 *** .01) ///
	stats( IQR spec N FSB  Weak_IV_KP,  ///
	label(	  /// 
	  "IQR" "Estimator" "Obs." "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
	fmt(  %04.3fc  %3s  %9.0fc   %04.3fc ))

noi di ""
noi di "***************************"
noi di "******** Panel B **********"
noi di "***************************"
noi esttab  lskill_`out'`i' lm_skill_`out'`i' lw_skill_`out'`i',    ///
	keep(ave_ols_ch  `pre'ln_skill_diff_`out'_ch  `pre'm_ln_skill_diff_`out'_ch  `pre'w_ln_skill_diff_`out'_ch ) ///
	se(%9.3f) b(%9.3f) star(* .1  ** .05 *** .01) ///
	stats( IQR spec N FSB  Weak_IV_KP,  ///
	label(	  /// 
	  "IQR" "Estimator" "Obs." "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
	fmt(  %04.3fc  %3s  %9.0fc   %04.3fc ))


* Panel A
esttab  skill_`out'`i' m_skill_`out'`i' w_skill_`out'`i'    ///
using "$results/table_A12_panelA.tex", ///
se ///
star(* .1  ** .05 *** .01) ///
stats( IQR spec N FSB  Weak_IV_KP,  ///
label(	  /// 
  "IQR" "Estimator" "Obs." "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
fmt(  %04.3fc  %3s  %9.0fc   %04.3fc ) ///
) ///			
mtitles("All" "Men" "Women") ///
nonum ///
mgroups("Panel A:  \$ \Delta \ln(\frac{Pay_i^{Non-Prod}}{Pay_i^{Prod}})_{t-1} \$" , ///    
prefix(\multicolumn{@span}{c}{) suffix(}) ///
span erepeat(\cline{@span}) /// 
) ///
keep(ave_ols_ch) ///
booktabs ///
substitute(\_ _) ///
label ///
nonotes /// 
replace

* Panel B				
esttab  lskill_`out'`i' lm_skill_`out'`i' lw_skill_`out'`i'    ///
using "$results/table_A12_panelB.tex", ///
se ///
star(* .1  ** .05 *** .01) ///
stats( IQR spec N FSB Weak_IV_KP,  ///
label(	  /// 
"IQR" "Estimator" "Obs." "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
fmt(  %04.3fc  %3s  %9.0fc   %04.3fc ) ///
) ///			
mtitles("All" "Men" "Women") ///
nonum ///
mgroups("Panel B:	\$ \Delta \ln(\frac{Pay_i^{Non-Prod}}{Pay_i^{Prod}})_{t} \$ ", ///    
prefix(\multicolumn{@span}{c}{) suffix(}) ///
span erepeat(\cline{@span}) /// 
) ///
booktabs ///
substitute(\_ _) ///
keep(ave_ols_ch  `pre'ln_skill_diff_`out'_ch   `pre'm_ln_skill_diff_`out'_ch  `pre'w_ln_skill_diff_`out'_ch ) ///
order(ave_ols_ch  `pre'ln_skill_diff_`out'_ch    `pre'm_ln_skill_diff_`out'_ch  `pre'w_ln_skill_diff_`out'_ch) ///
label ///
nonotes /// 
replace
				
}
	
*****************************************	
**# Table A13: Pay inequality by college 
*****************************************		 
qui {

**# Setup
use "analysis/ipums_industry_data.dta", clear
merge 1:1 ind1990 year using "analysis/ind1990_changes_rout1.dta"
* m=2: inds in ipums emp data that not in industry level data
* 122
* 140
* 301
* 350
* 392
assert _m!=1
keep if _m == 3
drop _merge
drop if inlist(ind1990,210,232,362,390)

glo IV "ave_ols_ch  = ave_iv_ch"
xtset ind1990 year

loc controls1 
loc controls2 `controls1' $tariff_controls
loc controls3 `controls2' $prod_controls
loc controls4 `controls3' $rti_controls

egen full = rowmiss(`controls4')
keep if full==0
drop full
glo IV "ave_ols_ch  = ave_iv_ch"
eststo clear	

**# Run regressions
loc out pay 
sum ave_ols_ch,d
loc iqrx `=`r(p75)'-`r(p25)'' 

loc i = 4

* All
reg  ave_ols_ch ave_iv_ch   `controls`i'', robust	
loc fsb = _b[ave_iv_ch]

sum ln_coll_diff_`out'_ch, d
loc iqry `=`r(p75)'-`r(p25)''

eststo coll_`out'`i': ivreg2 ln_coll_diff_`out'_ch ($IV) `controls`i'', robust	
eststo coll_`out'`i', add(UnderID_p  e(idp))
eststo coll_`out'`i', add(Weak_IV_KP e(widstat))
eststo coll_`out'`i', add(FSB        `fsb')
loc beta = _b[ave_ols_ch]
loc iqe = `=(`beta'*`iqrx')'
estadd  scalar IQR = `iqe'
estadd loc spec "IV"

* Men
sum m_ln_coll_diff_`out'_ch, d
loc iqry `=`r(p75)'-`r(p25)''

eststo m_coll_`out'`i': ivreg2 m_ln_coll_diff_`out'_ch ($IV) `controls`i'', robust	
eststo m_coll_`out'`i', add(UnderID_p  e(idp))
eststo m_coll_`out'`i', add(Weak_IV_KP e(widstat))
eststo m_coll_`out'`i', add(FSB        `fsb')
loc beta = _b[ave_ols_ch]
loc iqe = `=(`beta'*`iqrx')'
estadd  scalar IQR = `iqe'
estadd loc spec "IV"


* Women
sum w_ln_coll_diff_`out'_ch, d
loc iqry `=`r(p75)'-`r(p25)''

eststo w_coll_`out'`i': ivreg2 w_ln_coll_diff_`out'_ch ($IV) `controls`i'', robust	
eststo w_coll_`out'`i', add(UnderID_p  e(idp))
eststo w_coll_`out'`i', add(Weak_IV_KP e(widstat))
eststo w_coll_`out'`i', add(FSB        `fsb')
loc beta = _b[ave_ols_ch]
loc iqe = `=(`beta'*`iqrx')'
estadd  scalar IQR = `iqe'
estadd loc spec "IV"

			
**# Make table
noi di ""
noi di "**************************************************"
noi di "******************* Table A13 *********************"
noi di "**************************************************"
noi esttab  coll_`out'`i' m_coll_`out'`i' w_coll_`out'`i', ///
	keep(ave_ols_ch) se(%9.3f) b(%9.3f) star(* .1  ** .05 *** .01) ///
	stats(IQR  spec N  FSB  Weak_IV_KP,  ///
	label("IQE"	"Specification"  /// 
	"Obs."   "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
	fmt(%04.3fc  %3s %9.0fc   %04.3fc  %04.3fc ))

esttab  coll_`out'`i' m_coll_`out'`i' w_coll_`out'`i'    ///
using "$results/table_A13.tex", ///
se ///
star(* .1  ** .05 *** .01) ///
stats(IQR  spec N  FSB  Weak_IV_KP,  ///
label("IQE"	"Specification"  /// 
"Obs."   "\$1^{st}\$ Stage Coeff." "KP F-Stat") ///
fmt(%04.3fc  %3s %9.0fc   %04.3fc  %04.3fc ) ///
) ///	0
keep(ave_ols_ch) ///
mtitles("All" "Men" "Women") ///
booktabs ///
substitute(\_ _) ///
label ///
nonotes /// 
replace 

}	

************************************************
**# Table A14: Gender occupation specialization
************************************************
qui {
**# Setup
use "analysis/occ1990_occ1990dd_GLL.dta", clear
drop if _m==2
drop _m
	* 1 obs only in dorn concordance. Can't do anything with this "new" occ
drop if occGroup == .
keep occ1990dd occGroup
duplicates drop
merge 1:1 occ1990dd using "raw/occ1990dd_alm.dta"
keep if _m == 3
drop _m
keep occ1990dd occGroup RTI
egen occ_group_rti = mean(RTI), by(occG)
	* 1: Managers, professionals, technology, finance, public saftey
	* 2: Clerical, retail sales
	* 3: Low skill services
	* 4: Production, craft
	* 5: Machine operators, assemblers
	* 6: Transport, construction, mechanical, mining, farm
save "processing/occ_rti.dta", replace	
	
use "analysis/ipums_industry_data.dta", clear
merge 1:1 ind1990 year using "analysis/ind1990_changes_rout1"
	* m=2: inds in ipums emp data that not in industry level data
	* 122
	* 140
	* 301
	* 350
	* 392
assert _m!=1
keep if _m == 3
drop _merge
drop if inlist(ind1990,210,232,362,390)
keep ind1990
save "processing/industries_manuf", replace

use  "analysis/ipums_1970_1990", clear
keep if empstat == 1 & age >= 25 & age <= 65
keep if year == 1980
mmerge ind1990 using "processing/industries_manuf.dta", type(n:1)
assert _m!=2
keep if _m == 3
	* This keeps manuf inds
	* Note some occs get dropped here because they never appear in manuf inds
drop _m

joinby occ1990 using "analysis/occ1990_occ1990dd_weights.dta", unmatched(both)
	* m=1 are military
	* m=2 are only 2 obs	
assert occ1990==905 if _m==1
keep if _m == 3
drop _m
replace perwt = perwt*weight

mmerge occ1990dd using "processing/occ_rti.dta", type(n:1)
	* 20 m=2 obs: these are occs that aren't in any manuf ind (eg. occ1990 4 15 29) 
assert _m!=1
keep if _m==3

keep occ1990* RTI perwt* sex

gen women = (sex == 2)
gen men = (sex == 1)

gen women_perwt = women*perwt
gen men_perwt = men*perwt

bysort occ1990dd: egen mode_occ1990 = mode(occ1990)
label values mode_occ occ1990_lbl
collapse (mean) RTI (first) mode_occ (sum) women_perwt men_perwt, by(occ1990dd)
label values mode_occ1990 occ1990_lbl

egen women_tot = total(women_perwt)
egen men_tot = total(men_perwt)

gen women_share = women_perwt/women_tot
gen men_share = men_perwt/men_tot

gsort -women_share

egen mean_rti = mean(RTIa)
egen sd_rti = sd(RTIa)

gen normalized_rti = (RTIa-mean_rti)/sd_rti


**# Make table
noi di ""
noi di "**************************************************"
noi di "******************* Table A14 ********************"
noi di "**************************************************"
noi di ""
noi di "***************************"
noi di "******** Panel A **********"
noi di "***************************"
gsort -women_share
noi list occ1990dd mode_occ1990 normalized_rti women_share in 1/10

noi di ""
noi di "***************************"
noi di "******** Panel B **********"
noi di "***************************"
gsort -men_share
noi list occ1990dd mode_occ1990 normalized_rti men_share in 1/10
}



************************************
**# Fig A6: Pay densities by gender
************************************
qui {
**# Setup

*Identify Occupation Groups
use "analysis/occ1990_occ1990dd_GLL.dta", clear
drop if occGroup == .
keep occ1990 occGroup occupationD
duplicates drop
save "processing/occG.dta", replace	

use "analysis/ipums_industry_data.dta", clear
merge 1:1 ind1990 year using "analysis/ind1990_changes_rout1"
* m=2: inds in ipums emp data that not in industry level data
* 122
* 140
* 301
* 350
* 392
assert _m!=1
keep if _m == 3
drop _merge
drop if inlist(ind1990,210,232,362,390)

**# Make graph	
sum  m_ln_skill_diff_pay_ch  w_ln_skill_diff_pay_ch ,d
	// min and max are for women -- -.3, 0.9 spans both groups
gen xrange = -0.4 + (_n-1)*(1/50)
replace xrange = . if xrange>1
kdensity  m_ln_skill_diff_pay_ch, nograph gen(xm ym) at(xrange)
kdensity  w_ln_skill_diff_pay_ch, nograph gen(xw yw)  at(xrange) 
  
gen zero = 0
format ym yw xrange %04.3fc
tw rarea ym zero xrange 	, fc(blue%65) lc(blue%65) lp(dash) lw(thick) ///
|| rarea yw zero xrange 	, fc(green%65) lc(green%65) lp(solid) lw(thick) ///
xtitle("{&Delta}ln(Pay{sub:i}{sup:Non-Prod}/Pay{sub:i}{sup:Prod})") ///
legend(order( 1 "Men"  2 "Women") row(1) pos(6))  ///
ytitle(Density)

graph export "$figures/fig_A6.png", replace
}

*************************************	
**# Fig A7: Occupation HHI by gender
*************************************
qui {
**# Setup
use "analysis/ipums_industry_data.dta", clear
merge 1:1 ind1990 year using "analysis/ind1990_changes_rout1.dta"
* m=2: inds in ipums emp data that not in industry level data
* 122
* 140
* 301
* 350
* 392
assert _m!=1
keep if _m == 3
drop _merge
drop if inlist(ind1990,210,232,362,390)
keep ind1990 ave_iv_ch ave_ols_ch
tempfile ind_1990

save `ind_1990', replace

use  "analysis/ipums_1970_1990", clear
keep if empstat == 1 & age >= 25 & age <= 65 & occ1990!=905
assert occ1990!=999
keep if year==1980
* No obs with occ1990 = "N/A and unknown"
collapse (sum) perwt, by(occ1990 ind1990 sex)

preserve
	keep ind  occ1  
	duplicates drop ind  occ1  , force
	gen sex = 3
	gen perwt = 1
	tempfile uniform
	save `uniform' , replace
restore

append using `uniform'
merge m:1 ind1990 using `ind_1990'

bys sex _merge : egen temp = total(perwt)
bys sex : egen  emp = total(perwt)
gen eshare = temp/emp
egen first = tag(sex _m)

* Among employed persons, what share of men and women are in our ipums sample _m==3
list eshare _m sex if first
keep if _m==3
drop _merge-first
 
* Conditional on being in sample, how concentrated is employment?
merge m:1 occ using "processing/occG.dta", keepus(occG)
rename occG occG
drop if _m==2

bys sex : egen emp = total(perwt)

bys occ1990 sex : egen occ_emp = total(perwt)
bys ind1990 sex : egen ind_emp = total(perwt)
bys occG 	sex : egen occG_emp= total(perwt)

egen first_occ  = tag(occ1990 sex)
egen first_ind  = tag(ind1990 sex)
egen first_occG = tag(occG sex)
egen first_sex  = tag(sex)

gen ind_sh  = ind_emp/emp if first_ind==1
gen occ_sh  = occ_emp/emp if first_occ==1
gen occG_sh = occG_emp/emp if first_occG==1
gen ind_sh_sq  = ind_sh^2
gen occ_sh_sq  = occ_sh^2		
gen occG_sh_sq = occG_sh^2

bys sex : egen ind_hhi  = total(ind_sh_sq)
bys sex : egen occ_hhi  = total(occ_sh_sq)
bys sex : egen occG_hhi = total(occG_sh_sq)

gen mf = "m" if sex==1
replace mf = "f" if sex==2
replace mf = "u" if sex==3

keep ind_hhi occ_hhi occG_hhi sex first_sex

list ind_hhi occ_hhi occG_hhi sex if first_sex==1
keep if first_sex==1
graph bar occ_hhi ind_hhi occG_hhi, over(sex)

rename (occ_hhi ind_hhi occG_hhi) (hhi_occ hhi_ind hhi_occG)
reshape long hhi_, i(sex) j(new) string
replace hhi_ = hhi_*10000
format hhi %4.1fc

reshape wide hhi_, i(new) j(sex) 
rename (hhi_1 hhi_2 hhi_3) (Male Female Uniform)

	 
**# Make graph
graph bar Uniform Male Female, over(new, relabel(1 "Industry" 2 "Occupation" 3 "Occupation Group"))  ///
legend(order(1 "Uniform" 2 "Men" 3 "Women") pos(6) row(1)) ///
bar(1, color(gs2%10)) ///
bar(2, color(blue%65) lp(dash) lw(thick)) ///
bar(3, color(green%65) lp(solid) lw(thick))  ///
ytitle("HHI")
graph export "$figures/fig_A7.png", replace	
}	

log close